package com.zt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.api.UserInfoFeign;
import com.zt.cardbinding.CardBindingFeign;
import com.zt.service.CardBindingService;
import com.zt.service.CardTableService;
import com.zt.system.CardBinding;
import com.zt.system.TransactionRecord;
import com.zt.service.TransactionRecordService;
import com.zt.mapper.TransactionRecordMapper;
import com.zt.system.vo.CardTableVO;
import com.zt.user.UserFeign;
import com.zt.user.UserInfo;
import com.zt.utils.Result;
import com.zt.vo.TransactionRecordVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* @author 不.凌
* @description 针对表【transaction_record】的数据库操作Service实现
* @createDate 2024-09-03 17:43:26
*/
@Service
public class TransactionRecordServiceImpl extends ServiceImpl<TransactionRecordMapper, TransactionRecord>
    implements TransactionRecordService{

    @Resource
    private UserFeign userFeign;

    @Resource
    private CardBindingFeign cardBindingFeign;
    @Resource
    private UserInfoFeign userInfoFeign;
    @Resource
    private CardTableService cardTableService;
    @Resource
    private CardBindingService cardBindingService;

    @Override
    public Result PageTransactionRecord(TransactionRecordVo transactionRecordVo) {
        Page<TransactionRecord> page = new Page<>(transactionRecordVo.getPageNum(), transactionRecordVo.getPageSize());
        LambdaQueryWrapper<TransactionRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.checkValNotNull(transactionRecordVo.getName()),TransactionRecord::getName, transactionRecordVo.getName());
        this.page(page, wrapper);
        for (TransactionRecord record : page.getRecords()) {
            String name = userFeign.getUserById(record.getUserId());
            record.setName(name);
            String cardNumber = cardBindingFeign.getCardNumber(record.getBindingId());
            record.setCardNumber(cardNumber);
        }
        return Result.success(page);
    }
    //    根据信用卡id查询本月的消费总计
    @Override
    public List<TransactionRecord> transactionRecordByCardId(Integer cardId) {
        Date now = new Date();
        Date beginningOfMonth = getBeginningOfMonth(now);
        Date endOfMonth = getEndOfMonth(now);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("当前月初日期：" + sdf.format(beginningOfMonth));
        System.out.println("当前月末日期：" + sdf.format(endOfMonth));

        List<TransactionRecord> list = this.list(Wrappers.<TransactionRecord>lambdaQuery()
                .eq(cardId != null, TransactionRecord::getCardId, cardId)
                .eq(TransactionRecord::getTransactionType,2)
                .between(TransactionRecord::getTransactionDate, beginningOfMonth, endOfMonth)
        );
        return list;
    }

    @Override
    @Transactional
    public Result transactionRecordSave(CardTableVO cardTableVO) {
        System.out.println(cardTableVO);
//        获取当前用户id
        UserInfo userInfoUserId = userInfoFeign.getUserInfoUserId();
        CardBinding cardBinding = cardBindingService.cardBindingByCardNumber(cardTableVO.getCardNumber());

        TransactionRecord transactionRecord = new TransactionRecord();
        transactionRecord.setUserId(userInfoUserId.getUserId());
        transactionRecord.setTransactionType(2);
        transactionRecord.setTransactionDate(new Date());
        transactionRecord.setAmount(cardTableVO.getMount());
        transactionRecord.setCardId(cardTableVO.getCardId());
        transactionRecord.setBindingId(cardBinding.getBindingId());
        boolean save = this.save(transactionRecord);
        cardBindingService.cardBindingByBindingIdUpdate(cardTableVO);
        cardTableService.cardTableByCreditScore();
        return Result.success("成功");
    }

    @Override
    @Transactional
    public Result transactionRecordPayBack(CardTableVO cardTableVO) {
        System.out.println(cardTableVO);
        UserInfo userInfoUserId = userInfoFeign.getUserInfoUserId();
//        修改银行卡
        cardBindingService.cardBindingPayBackByBindingId(cardTableVO);
        TransactionRecord transactionRecord = new TransactionRecord();
        transactionRecord.setUserId(userInfoUserId.getUserId());
        transactionRecord.setTransactionType(4);
        transactionRecord.setAmount(cardTableVO.getMount());
        transactionRecord.setTransactionDate(new Date());
        transactionRecord.setCardId(cardTableVO.getCardId());
        CardBinding cardBinding = cardBindingService.cardBindingByCardNumber(cardTableVO.getCardNumber());
        transactionRecord.setBindingId(cardBinding.getBindingId());
        this.save(transactionRecord);
        cardTableService.cardTableByCreditScore();
        return Result.success("成功");
    }

    @Override
    public List<TransactionRecord> transactionPayBackRecordByCardId(Integer cardId) {
        Date now = new Date();
        Date beginningOfMonth = getBeginningOfMonth(now);
        Date endOfMonth = getEndOfMonth(now);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("当前月初日期：" + sdf.format(beginningOfMonth));
        System.out.println("当前月末日期：" + sdf.format(endOfMonth));

        List<TransactionRecord> list = this.list(Wrappers.<TransactionRecord>lambdaQuery()
                .eq(cardId != null, TransactionRecord::getCardId, cardId)
                .eq(TransactionRecord::getTransactionType,4)
                .between(TransactionRecord::getTransactionDate, beginningOfMonth, endOfMonth)
        );
        return list;
    }

    //    封装月初时间
    public static Date getBeginningOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
    //    封装月末时间
    public static Date getEndOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }
}